package com.lh.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lh.entity.table.LeadLeader;
import com.lh.entity.table.RectificationInfo;
import com.lh.entity.table.ReminderPerson;
import com.lh.mapper.LeadLeaderMapper;
import com.lh.mapper.ReminderPersonMapper;
import com.lh.service.LeadLeaderService;
import com.lh.utils.Answer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class LeadLeaderServiceImpl implements LeadLeaderService {
    @Resource
    private LeadLeaderMapper leadLeaderMapper;
    @Resource
    private ReminderPersonMapper reminderPersonMapper;

    @Override
    public Answer<Object> getLeadLeaderPage(JSONObject reqJson) {
        Integer pageSize = reqJson.getInteger("pageSize"); // 默认10条/页
        Integer pageNum = reqJson.getInteger("pageNum");    // 默认第1页
        if (pageNum < 1) pageNum = 1; // 防止pageNum为0或负数
        Page<LeadLeader> queryPage = new Page<>(pageNum, pageSize);
        String leaderName = reqJson.getString("leaderName");
        String department = reqJson.getString("department");
        LambdaQueryWrapper<LeadLeader> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(leaderName)){
            queryWrapper.like(LeadLeader::getLeaderName,leaderName);
        }
        if (StringUtils.isNotBlank(department)){
            queryWrapper.like(LeadLeader::getDepartment,department);
        }
        Page<LeadLeader> leadLeaderPage = leadLeaderMapper.selectPage(queryPage, queryWrapper);
        return Answer.ok(leadLeaderPage);
    }

    @Override
    public Answer<Object> getLeadLeaderList() {
        List<LeadLeader> leadLeaders = leadLeaderMapper.selectList(new LambdaQueryWrapper<>());
        List<String> leaderNameList = leadLeaders.stream()
                // 过滤 null：避免个别领导姓名为 null 导致列表中出现 null 元素（可选，根据业务需求）
                .filter(leader -> leader != null && StringUtils.isNotBlank(leader.getLeaderName()))
                // 提取 leaderName 字段值
                .map(LeadLeader::getLeaderName)
                // 收集为 List<String>
                .collect(Collectors.toList());
        return Answer.ok(leaderNameList);
    }

    @Override
    public Answer<Object> addLeadLeader(LeadLeader leadLeader) {
        String leaderName = leadLeader.getLeaderName();
        LeadLeader leadLeaders = leadLeaderMapper.selectOne(new LambdaQueryWrapper<LeadLeader>().eq(LeadLeader::getLeaderName, leaderName));
        if (leadLeaders!=null){
            return Answer.ok("领导姓名已存在");
        }
        ReminderPerson reminderPerson = new ReminderPerson();
        reminderPerson.setPersonName(leadLeader.getLeaderName());
        reminderPerson.setPhone(leadLeader.getLeaderPhone());
        reminderPerson.setUnitName(leadLeader.getDepartment());
        reminderPerson.setRemark(leadLeader.getSecretaryName());
        leadLeaderMapper.insert(leadLeader);
        reminderPersonMapper.insert(reminderPerson);
        return Answer.ok("添加成功");
    }

    @Override
    public Answer<Object> updateLeadLeader(LeadLeader leadLeader) {
        String leaderName = leadLeader.getLeaderName();
        LeadLeader leadLeaders = leadLeaderMapper.selectOne(new LambdaQueryWrapper<LeadLeader>().eq(LeadLeader::getLeaderName, leaderName));
        if (leadLeaders==null){
            return Answer.ok("领导姓名不存在，请先添加领导");
        }
        leadLeaderMapper.updateById(leadLeader);
        return Answer.ok("更新成功");
    }

    @Override
    public Answer<Object> deleteLeadLeaderById(JSONObject reqJson) {
        Integer id = reqJson.getInteger("id");
        leadLeaderMapper.deleteById(id);
        return Answer.ok("删除成功");
    }


}
